$(function(){
    $('nav#menu_left').mmenu();
    /*$('.act_time_node').live('click', function(e){
        var id = e.currentTarget.attributes['data-id'].value;
        window.location.href = '/invite/'+id;
    })*/
    $('#startAct').on('click', function(){
        window.location.href = '/create'
    })
    $('#startAct2').on('click', function(){
        window.location.href = '/create'
    })
    $('#loadmore').on('click', function(){
        var btn = $(this);
        AlertBox.showTip('loading')
        $.post('/fetchActs', {
            pageNo: $('#pageNo').val()
        }, function(data){
            AlertBox.hideTip();
            if(data.acts.length > 0){
                $('#pageNo').val(data.pageNo);
                _buildAppendHtml(data.acts, function(html){
                    $('#content').append(html);
                })
            }else{
                btn.html('没有啦~')
            }
        })
    })
})
$.fn.fadeInWithDelay = function(){
    var delay = 0;
    return this.each(function(){
        $(this).delay(delay).animate({opacity:1}, 200);
        delay += 100;
    });
};
function _buildAppendHtml(acts, cb){
    var html = '';
    for(var i= 0,len=acts.length; i<len; i++){
        var act = acts[i];
        var date = new Date(act.startTime);
        var dateZh = date.format('yyyy年MM月dd号')+'('+date.getZhDay(date)+')'
        if(act.creator.id == userId){
            html += '<div class="row act_time_node" data-id="'+act._id+'">\
                            <div class="col-xs-12 node_panel">\
                                <div class="node_r">\
                                    <span class="node_arrows_r">◆</span>\
                                    <p class="act_name"><a href="/invite/'+act._id+'">'+act.name+'</a></p>\
                                    <p class="act_users">\
                                        <img src="/img/'+act.creator.id+'" class="img-circle act_headimg" onError="this.src=\'/fj_default_headimg\';"/> '+act.creator.name+'\
                                    </p>\
                                </div>\
                                <div class="node_mid"><span class="icon-circle-full act_dian"></span></div>\
                                <div class="node_l" style="text-align: left">\
                                        <label class="act_time">'+dateZh+'</label>\
                                </div>\
                                <div class="clearfix"></div>\
                            </div>\
                        </div>';
        }else{
            html += '<div class="row act_time_node" data-id="'+act._id+'">\
                            <div class="col-xs-12 node_panel">\
                                <div class="node_l" style="text-align: left">\
                                        <label class="act_time">'+dateZh+'</label>\
                                </div>\
                                <div class="node_mid"><span class="icon-circle-full act_dian"></span></div>\
                                <div class="node_r">\
                                    <span class="node_arrows_l">◆</span>\
                                    <p class="act_name"><a href="/invite/'+act._id+'">'+act.name+'</a></p>\
                                    <p class="act_users">\
                                        <img src="/img/'+act.creator.id+'" class="img-circle act_headimg" onError="this.src=\'/fj_default_headimg\';"/> '+act.creator.name+'\
                                    </p>\
                                </div>\
                                <div class="clearfix"></div>\
                            </div>\
                        </div>';
        }
    }
    cb(html);
}
